//
//  LGAddCustomMedicineController.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/7/24.
//

#import "LGAddCustomMedicineController.h"
//搜索药品
#import "LGMedicineManageController.h"
#import "LGCommunityPublishCell.h"
#import "LGAddMedicineTimeCell.h"
#import "LGAddMedicinHeadView.h"
#import "LGCustomTextView.h"
#import "LGPickerView.h"

#import "LGPhotoWayView.h"
#import <AssetsLibrary/AssetsLibrary.h>
#import "LQImgPickerActionSheet.h"
#import <AssetsLibrary/AssetsLibrary.h>
#import <Photos/Photos.h>
#import "TZImagePickerController.h"
#import "TZImageManager.h"
#import "TZVideoPlayerController.h"
#import "TZPhotoPreviewController.h"
#import "TZGifPhotoPreviewController.h"
#import "TZLocationManager.h"
#import "SDPhotoBrowser.h"
#import "LGCheckPhotoTool.h"
#import "HXCustomNavigationController.h"
#import <MobileCoreServices/MobileCoreServices.h>
#import <AVFoundation/AVFoundation.h>
#import <MediaPlayer/MediaPlayer.h>
#define placeHolder  @"请输入..."
#define itemCount  ((isPad == YES) ? 6 : 3)
#define itemWidth  ((Screen_W-viewPix(31)-viewPix(8)*(itemCount-1))/itemCount)
@interface LGAddCustomMedicineController ()<UITableViewDelegate,UITableViewDataSource,LGTextViewDelegate,UICollectionViewDelegate,UICollectionViewDataSource,UIImagePickerControllerDelegate,UIAlertViewDelegate,UINavigationControllerDelegate,HXCustomNavigationControllerDelegate>
/**   */
@property (nonatomic , strong)UITableView *tableView;
/**   */
@property (nonatomic , strong)UITextField *nameTF;
/**   */
@property (nonatomic , strong)LGCustomTextView *remarkTF;
/**   */
@property (nonatomic , strong)UILabel *photoTitleLabel;
/**   */
@property (nonatomic , strong)UILabel *photoTipLabel;
/**   */
@property (nonatomic , strong)UICollectionView *collectionView;
/**   */
@property (nonatomic , strong)NSArray *sectionArray;
/**  服药频率 */
@property (nonatomic , strong)NSArray *rateArray;
/**  时段 */
@property (nonatomic , strong)NSArray *timeArray;
/**  剂量 */
@property (nonatomic , strong)NSArray *doseArray;
/**   */
@property (nonatomic , strong)NSMutableDictionary *resultDic;
/**   */
@property (nonatomic , strong)NSMutableArray *fileArray;
/**   */
@property (nonatomic , assign)CGFloat remarkHeight;
/**   */
@property (nonatomic , assign)CGFloat collectionHeight;

@end

@implementation LGAddCustomMedicineController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.remarkHeight = viewPix(102);
    self.title = (_medicineId && _medicineId.length>0)?@"编辑用药":@"添加用药";
    self.medicineId = (_medicineId && _medicineId.length>0)?_medicineId:@"";
    self.tempPrescripid = (_tempPrescripid && _tempPrescripid.length>0)?_tempPrescripid:@"";
    self.sectionArray = @[
        @{@"title":@"药品名称",@"showTip":@"N",@"showLine":@"N",@"key":@""},
        @{@"title":@"服药频率",@"showTip":@"Y",@"showLine":@"N",@"key":@"take_frequency"},
        @{@"title":@"时段、剂量",@"showTip":@"N",@"showLine":@"Y",@"key":@""},
        @{@"title":@"备注",@"showTip":@"N",@"showLine":@"N",@"key":@""}];
    self.timeArray = @[@{@"title":@"空腹",@"key":@"take_dose_zero"},@{@"title":@"早饭后",@"key":@"take_dose_one"},@{@"title":@"中饭后",@"key":@"take_dose_two"},@{@"title":@"晚饭后",@"key":@"take_dose_three"},@{@"title":@"睡前",@"key":@"take_dose_four"},@{@"title":@"必要时",@"key":@"take_dose_five"}];
    [self.view addSubview:self.tableView];
    [self createButtomView];
    [self requestData];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self resetNavigationTheme];
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self resetNavigationTheme];
}

-(void)requestData{
    [RequestUtil withPOST:@"/doctor/Sportsprescription.MedicinePrescription/medicineDetail" parameters:@{@"temp_prescription_id":self.tempPrescripid,@"prescription_medicine_id":self.medicineId} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
        if(code == 200){
            self.nameTF.text = LGNSString(returnData[@"name"]);
            self.fileArray = [NSMutableArray arrayWithArray:returnData[@"picurls"]];
            self.remarkTF.content = LGNSString(returnData[@"remark"]);
            self.resultDic = [NSMutableDictionary dictionaryWithDictionary:returnData];
            [self.resultDic removeObjectForKey:@"take_dose_select"];
            [self.resultDic removeObjectForKey:@"take_frequency_select"];
            NSMutableArray *doseArray = [NSMutableArray array];
            NSMutableArray *rateArray = [NSMutableArray array];
            NSArray *takeDose = returnData[@"take_dose_select"];
            NSArray *takeRate = returnData[@"take_frequency_select"];
            for (NSInteger i=0; i<takeDose.count; i++) {
                [doseArray addObject:LGNSString(takeDose[i][@"title"])];
            }
            
            for (NSInteger i=0; i<takeRate.count; i++) {
                [rateArray addObject:LGNSString(takeRate[i][@"title"])];
            }
            self.doseArray = doseArray;
            self.rateArray = rateArray;
            [self changeCollectionViewHeight];
            [self.tableView reloadData];
            [self.collectionView reloadData];
        }
    } error:^(NSError *error) {
        
    }];
}

//药品名称
//-(void)selectMedicineName{
//    LGSearchMedicineController *controller = [[LGSearchMedicineController alloc]init];
//    controller.patientid = self.patientid;
//    controller.canSelect = YES;
//    [self.navigationController pushViewController:controller animated:YES];
//    __weak typeof(self) weakSelf = self;
//    controller.selectMedicineResult = ^(NSDictionary * _Nonnull dataDic) {
//        [weakSelf.resultDic setValue:LGNSString(dataDic[@"medicine_id"]) forKey:@"medicine_id"];
//        [weakSelf.resultDic setValue:LGNSString(dataDic[@"unit"]) forKey:@"unit"];
//        [weakSelf.resultDic setValue:LGNSString(dataDic[@"name"]) forKey:@"name"];
//        [weakSelf.tableView reloadData];
//    };
//}

//服药频率
-(void)selectMedicineRate{
    [self.nameTF resignFirstResponder];
    if(self.rateArray.count>0){
        LGPickerView *pickerView = [[LGPickerView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, Screen_H)];
        pickerView.titleLabel.text = @"服药频率";
        pickerView.pickArry = @[self.rateArray];
        pickerView.selectStr = (LGNSString(self.resultDic[@"take_frequency"]).length>0)?LGNSString(self.resultDic[@"take_frequency"]):LGNSString(self.rateArray[0]);
        [self.view.window addSubview:pickerView];
        __weak typeof(self) weakSelf = self;
        pickerView.selectPickViewResult = ^(NSArray *resultArray) {
            [weakSelf.resultDic setValue:LGNSString(resultArray[0]) forKey:@"take_frequency"];
            [weakSelf.tableView reloadData];
        };
    }else{
        [self requestData];
    }
}

//保存
-(void)saveMedicineAction{
    NSString *frequency = LGNSString(self.resultDic[@"take_frequency"]);
    NSString *take_zero = LGNSString(self.resultDic[@"take_dose_zero"]);
    NSString *take_one = LGNSString(self.resultDic[@"take_dose_one"]);
    NSString *take_two = LGNSString(self.resultDic[@"take_dose_two"]);
    NSString *take_three = LGNSString(self.resultDic[@"take_dose_three"]);
    NSString *take_four = LGNSString(self.resultDic[@"take_dose_four"]);
    NSString *take_five = LGNSString(self.resultDic[@"take_dose_five"]);
    
    if(self.nameTF.text.length == 0){
        [TooltipView showMessage:@"请选择服用药物" offset:0];
        return;
    }
    
    if(frequency.length == 0){
        [TooltipView showMessage:@"请选择服药频率" offset:0];
        return;
    }
    
    if(take_one.length == 0 && take_two.length == 0 && take_three.length == 0 && take_four.length == 0 && take_zero.length == 0 && take_five.length == 0){
        [TooltipView showMessage:@"请选择服药时段、剂量" offset:0];
        return;
    }
    
    //保存药物
    NSMutableDictionary *tempDic = [NSMutableDictionary dictionaryWithDictionary:self.resultDic];
    [tempDic setValue:self.tempPrescripid forKey:@"temp_prescription_id"];
    [tempDic setValue:self.patientid forKey:@"patient_id"];
    [tempDic setValue:self.nameTF.text forKey:@"pic_name"];
    [tempDic setValue:self.fileArray forKey:@"picurls"];
    
    [RequestUtil withPOST:@"/doctor/Sportsprescription.MedicinePrescription/medicineSave" parameters:tempDic withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
        if(code == 200){
            [TooltipView showMessage:@"保存成功" offset:0];
            for (UIViewController *controller in self.navigationController.viewControllers) {
                if([controller isKindOfClass:[LGMedicineManageController class]]){
                    [self.navigationController popToViewController:controller animated:YES];
                    return;
                }
            }
        }else{
            [TooltipView showMessage:message offset:0];
        }
    } error:^(NSError *error) {
        [TooltipView showMessage:LGErrorMessage offset:0];
    }];
}



//删除
-(void)delegateMedicienAction{
    __weak typeof(self) weakSelf = self;
    [LGAlertTool alertWithTitle:@"确认删除这条药物吗？" sureAction:^{
        [RequestUtil withPOST:@"/doctor/Sportsprescription.MedicinePrescription/medicineDelete" parameters:@{@"prescription_medicine_id":weakSelf.medicineId} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
            if(code == 200){
                [TooltipView showMessage:@"删除成功" offset:0];
                [weakSelf.navigationController popViewControllerAnimated:YES];
            }else{
                [TooltipView showMessage:message offset:0];
            }
        } error:^(NSError *error) {
            [TooltipView showMessage:LGErrorMessage offset:0];
        }];
    }];
}


-(void)textViewValueChanged:(NSString *)content{
    [self.resultDic setValue:content forKey:@"remark"];
}

-(void)textViewHeightChanged:(CGFloat)height{
    if(height != self.remarkHeight  && height>=viewPix(102)){
        self.remarkHeight = height;
        self.remarkTF.frame = CGRectMake(viewPix(28), viewPix(5), Screen_W-viewPix(56), height);
        [self.tableView reloadData];
    }
}

#pragma mark
#pragma mark ====> 创建控件
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 4;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return (section == 2)?self.timeArray.count:0;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LGAddMedicineTimeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.dataDic = self.timeArray[indexPath.row];
    cell.resultDic = self.resultDic;
    __weak typeof(self) weakSelf = self;
    cell.clearSelectResult = ^(NSString * _Nonnull key) {
        [weakSelf.resultDic setValue:@"" forKey:key];
        [weakSelf.tableView reloadData];
    };
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [self.nameTF resignFirstResponder];
    //安全判断
    if (self.doseArray.count == 0) {
        [self requestData];
        return;
    }
    __block NSString *key = LGNSString(self.timeArray[indexPath.row][@"key"]);
    LGPickerView *pickerView = [[LGPickerView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, Screen_H)];
    pickerView.titleLabel.text = @"选择剂量";
    pickerView.pickArry = @[self.doseArray];
    pickerView.selectStr = (LGNSString(self.resultDic[key]).length>0)?LGNSString(self.resultDic[key]):@"1";
    [[ObjectTool mainWindow] addSubview:pickerView];
    __weak typeof(self) weakSelf = self;
    pickerView.selectPickViewResult = ^(NSArray *resultArray) {
        NSString *content = LGNSString(resultArray[0]);
        [weakSelf.resultDic setValue:content forKey:key];
        [weakSelf.tableView reloadData];
    };
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return (section<2)?viewPix(70):viewPix(52);
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    CGFloat height = (section<2)?viewPix(70):viewPix(52);
    LGAddMedicinHeadView *headView = [[LGAddMedicinHeadView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, height)];
    headView.dataDic = self.sectionArray[section];
    headView.resultDic = self.resultDic;
    if(section == 0){
        [headView addSubview:self.nameTF];
    }else if (section == 1){
        [headView.actionBtn addTarget:self action:@selector(selectMedicineRate) forControlEvents:UIControlEventTouchUpInside];
    }
    return headView;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if(section == 0){
        return viewPix(80)+self.collectionHeight;
    }else if (section == 1){
        return 0.01;
    }else if (section == 2){
        return viewPix(15);
    }else{
        return self.remarkHeight+viewPix(20);
    }
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    if(section == 0){
        UIView *footView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(80)+self.collectionHeight)];
        [footView addSubview:self.photoTitleLabel];
        [footView addSubview:self.photoTipLabel];
        [footView addSubview:self.collectionView];
        return footView;
    }else{
        CGFloat height = 0.01;
        if (section == 1){
            height = 0.01;
        }else if (section == 2){
            height =  viewPix(15);
        }else{
            height =  self.remarkHeight+viewPix(20);
        }
        LGCornerFooterView *footView = [[LGCornerFooterView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, height)];
        footView.bottomMargin = viewPix(8);
        if(section == 3){
            [footView addSubview:self.remarkTF];
        }
        return footView;
    }
    
}

//删除图片、视频
-(void)deleteFileAction:(UIButton *)sender{
    [self.nameTF resignFirstResponder];
    NSInteger index = sender.tag-2000;
    [self.fileArray removeObjectAtIndex:index];
    [self.collectionView reloadData];
    [self changeCollectionViewHeight];
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return (self.fileArray.count<9)?self.fileArray.count+1:9;
}


-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    LGCommunityPublishCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.playButton.tag = 1000+indexPath.item;
    cell.deleteBtn.tag = 2000+indexPath.item;
    [cell.deleteBtn addTarget:self action:@selector(deleteFileAction:) forControlEvents:UIControlEventTouchUpInside];
    if(indexPath.item >= self.fileArray.count){
        cell.type = @"add";
        cell.imageView.image = [UIImage imageNamed:@"用药管理-添加图片"];
    }else {
        cell.type = @"image";
        if ([self.fileArray[indexPath.item] isKindOfClass:[UIImage class]]) {
            cell.imageView.image = self.fileArray[indexPath.item];
        }else{
            [cell.imageView sd_setImageWithURL:[NSURL URLWithString:LGNSString(self.fileArray[indexPath.item])]];
        }
    }
    return cell;
}
    

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    [self.nameTF resignFirstResponder];
    if(indexPath.item >= self.fileArray.count){
        //添加图片
        [self addNewImage];
        
    }else {
        //查看图片
        LGCheckPhotoTool *photoView = [[LGCheckPhotoTool alloc]initWithFrame:CGRectMake(0, 0, Screen_W, Screen_H)];
        photoView.picArray = self.fileArray;
        photoView.title = @"";
        [[ObjectTool mainWindow] addSubview:photoView];
        [photoView showViewAnimation:CGPointMake(Screen_W/2.0, Screen_H/2.0) index:indexPath.item];
    }
}
    

#pragma mark - 选择图片
- (void)addNewImage{
    LGPhotoWayView *wayView = [[LGPhotoWayView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, Screen_H)];
    [[ObjectTool mainWindow] addSubview:wayView];
    __weak typeof(self) weakSelf = self;
  
    wayView.LGSelectPhotoWay = ^(NSString * _Nonnull selectWay) {
        if([selectWay isEqualToString:@"camera"]){
            //相机-拍摄
            if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
                UIImagePickerController *pickController = [[UIImagePickerController alloc]init];
                pickController.sourceType = UIImagePickerControllerSourceTypeCamera;
                pickController.delegate = self;
                pickController.allowsEditing = YES;
                pickController.modalPresentationStyle = UIModalPresentationFullScreen;
                [weakSelf presentViewController:pickController animated:YES completion:nil];
            }
        }else{
            //相册
            HXPhotoManagerSelectedType type = HXPhotoManagerSelectedTypePhoto;
            HXPhotoManager *manager = [[HXPhotoManager alloc] initWithType:type];
            manager.configuration.requestImageAfterFinishingSelection = YES;
            manager.configuration.exportVideoURLForHighestQuality = YES;
            manager.configuration.languageType = HXPhotoLanguageTypeSc;
            manager.configuration.type = HXConfigurationTypeWXChat;
            manager.configuration.singleSelected = NO;
            manager.configuration.photoCanEdit = NO;
            manager.configuration.photoMaxNum = 9-self.fileArray.count;
            manager.configuration.videoMaxNum = 1;
            if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
                HXCustomNavigationController *nav = [[HXCustomNavigationController alloc] initWithManager:manager delegate:self];
                nav.modalPresentationStyle = UIModalPresentationOverFullScreen;
                nav.modalPresentationCapturesStatusBarAppearance = YES;
                [weakSelf presentViewController:nav animated:YES completion:nil];
            }
        }
    };
}

//相册选完图片
- (void)photoNavigationViewController:(HXCustomNavigationController *)photoNavigationViewController didDoneAllList:(NSArray<HXPhotoModel *> *)allList photos:(NSArray<HXPhotoModel *> *)photoList videos:(NSArray<HXPhotoModel *> *)videoList original:(BOOL)original{
//    self.manager = nil;
    NSMutableArray *tempArray = [NSMutableArray array];
    if(photoList.count>0){
        for (HXPhotoModel *model in photoList) {
            [tempArray addObject:model.previewPhoto];
        }
        [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        [RequestUtil uploadImage:@"/doctor/upload/image" parameters:@{} withSign:YES indexName:@"photo[]" imageArray:tempArray success:^(NSInteger code, NSString *message, id returnData) {
            [MBProgressHUD hideHUDForView:self.view animated:YES];
            if(code == 200){
                NSArray *tempArray2 = returnData;
                NSMutableArray *imageArray = [NSMutableArray array];
                for (NSDictionary *tempDic in tempArray2) {
                    [imageArray addObject:LGNSString(tempDic[@"uri"])];
                }
                [self.fileArray addObjectsFromArray:imageArray];
                [self.collectionView reloadData];
                [self changeCollectionViewHeight];
            }else{
                [TooltipView showMessage:message offset:0];
            }
        } failure:^(NSError *error) {
            [MBProgressHUD hideHUDForView:self.view animated:YES];
        }];
    }
}


#pragma mark - TZImagePickerController
- (void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    [picker dismissViewControllerAnimated:YES completion:nil];
    NSString *type = [info objectForKey:UIImagePickerControllerMediaType];
    if ([type isEqualToString:@"public.image"]) {
        UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
        [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        [RequestUtil uploadImage:@"/doctor/upload/image" parameters:@{} withSign:YES indexName:@"photo[]" imageArray:@[image] success:^(NSInteger code, NSString *message, id returnData) {
            [MBProgressHUD hideHUDForView:self.view animated:YES];
            if(code == 200){
                NSArray *tempArray = returnData;
                NSMutableArray *imageArray = [NSMutableArray array];
                for (NSDictionary *tempDic in tempArray) {
                    [imageArray addObject:LGNSString(tempDic[@"uri"])];
                }
                [self.fileArray addObjectsFromArray:imageArray];
                [self.collectionView reloadData];
                [self changeCollectionViewHeight];
            }else{
                [TooltipView showMessage:message offset:0];
            }
        } failure:^(NSError *error) {
            [MBProgressHUD hideHUDForView:self.view animated:YES];
        }];
    }
}

//拍照返回的图片
- (void)refreshCollectionViewWithAddedAsset:(id)asset image:(UIImage *)image{
    [self.fileArray addObject:image];
    [self.collectionView reloadData];
    [self changeCollectionViewHeight];
}



#pragma mark - 改变view，collectionView高度
- (void)changeCollectionViewHeight{
    NSInteger count = (self.fileArray.count<9)?self.fileArray.count+1:self.fileArray.count;
    NSInteger line = count/itemCount;
    line = (count%itemCount)>0?line+1:line;
    line = (line>0)?line:1;
    CGFloat height = line*(itemWidth+viewPix(8))-viewPix(8);
    self.collectionHeight = height;
    self.collectionView.frame = CGRectMake(viewPix(15), viewPix(70), Screen_W-viewPix(30), height);
    [self.tableView reloadData];
}


-(UITableView *)tableView{
    if(!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, Screen_W, Screen_H-topBarHeight-viewPix(70)-bottomSafeBarHeight/4.0) style:UITableViewStyleGrouped];
        [_tableView registerClass:[LGAddMedicineTimeCell class] forCellReuseIdentifier:@"cell"];
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(8))];
    }
    return _tableView;
}

-(UITextField *)nameTF{
    if(!_nameTF){
        _nameTF = [[UITextField alloc]initWithFrame:CGRectMake(viewPix(114), viewPix(8), Screen_W-viewPix(150), viewPix(54))];
        _nameTF.tintColor = [UIColor colorWithString:@"#282828"];
        _nameTF.textColor = [UIColor colorWithString:@"#282828"];
        _nameTF.textAlignment = NSTextAlignmentRight;
        _nameTF.keyboardType = UIKeyboardTypeDefault;
        _nameTF.font = LGFontWeight(14, UIFontWeightMedium);
        _nameTF.attributedPlaceholder = [@"请输入" attributeString:@{NSFontAttributeName:LGFont(15),NSForegroundColorAttributeName:[UIColor colorWithString:@"999999"]} range:NSMakeRange(0, 3)];
        
    }
    return _nameTF;
}

-(LGCustomTextView *)remarkTF{
    if(!_remarkTF){
        _remarkTF = [LGCustomTextView textViewWithPlaceHolder:@"请输入..." textColor:@"#606266" textFont:LGFont(16) textAlignment:NSTextAlignmentLeft keyboard:UIKeyboardTypeDefault];
        _remarkTF.frame = CGRectMake(viewPix(28), viewPix(5), Screen_W-viewPix(56), viewPix(102));
        _remarkTF.contentInset = UIEdgeInsetsMake(viewPix(12), viewPix(12), viewPix(12), viewPix(12));
        _remarkTF.backgroundColor = [UIColor colorWithString:@"#F5F6FA"];
        _remarkTF.cornerRidus = 8;
        _remarkTF.customDelegate = self;
    }
    return _remarkTF;
}

-(UILabel *)photoTitleLabel{
    if(!_photoTitleLabel){
        _photoTitleLabel = [UILabel lableWithText:@"请上传正在服用的药物照片" colorString:@"#282828" textFont:LGFontWeight(16, UIFontWeightMedium) textAlignment:NSTextAlignmentLeft lines:1];
        _photoTitleLabel.frame = CGRectMake(viewPix(20), viewPix(10), Screen_W-viewPix(40), viewPix(22));
    }
    return _photoTitleLabel;
}

-(UILabel *)photoTipLabel{
    if(!_photoTipLabel){
        _photoTipLabel = [UILabel lableWithText:@"请确保药物规格清晰可见" colorString:@"#282828" textFont:LGFont(14) textAlignment:NSTextAlignmentLeft lines:1];
        _photoTipLabel.frame = CGRectMake(viewPix(20), viewPix(8)+kMaxY(self.photoTitleLabel.frame), Screen_W-viewPix(40), viewPix(22));
    }
    return _photoTipLabel;
}

-(UICollectionView *)collectionView{
    if(!_collectionView){
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
        layout.minimumInteritemSpacing = viewPix(8);
        layout.minimumLineSpacing = viewPix(8);
        layout.itemSize = CGSizeMake(itemWidth, itemWidth);
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(viewPix(15), viewPix(70), Screen_W-viewPix(30), 0) collectionViewLayout:layout];
        [_collectionView registerClass:[LGCommunityPublishCell class] forCellWithReuseIdentifier:@"cell"];
        _collectionView.backgroundColor = [UIColor clearColor];
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
    }
    return _collectionView;
}

-(void)createButtomView{
    UIView *bottomView = [[UIView alloc]initWithFrame:CGRectMake(0, kMaxY(self.tableView.frame), Screen_W, Screen_H-kMaxY(self.tableView.frame)-topBarHeight)];
    bottomView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:bottomView];
    UIButton *saveButton = [UIButton buttonWithTitle:@"保存" titleFont:LGFontWeight(18, UIFontWeightMedium) normalColor:@"#FFFFFF" selectColor:@"#FFFFFF"];
    saveButton.frame = CGRectMake(viewPix(16), viewPix(8), Screen_W-viewPix(32), viewPix(48));
    saveButton.backgroundColor = [UIColor colorWithString:kThemeColor];
    saveButton.cornerRidus = 10;
    [saveButton addTarget:self action:@selector(saveMedicineAction) forControlEvents:UIControlEventTouchUpInside];
    [bottomView addSubview:saveButton];
    if(_medicineId && _medicineId.length>0){
        UIButton *deleteBtn = [UIButton buttonWithTitle:@"删除" titleFont:LGFontWeight(16, UIFontWeightSemibold) textColor:@"#DD3030" imageName:@""];
        deleteBtn.frame = CGRectMake(Screen_W/2.0 - viewPix(10) - (Screen_W-viewPix(95))/2.0, viewPix(8), (Screen_W-viewPix(95))/2.0, viewPix(48));
        deleteBtn.layer.borderColor = [UIColor colorWithString:@"#DD3030"].CGColor;
        deleteBtn.layer.borderWidth = 1;
        deleteBtn.cornerRidus = 10;
        [deleteBtn addTarget:self action:@selector(delegateMedicienAction) forControlEvents:UIControlEventTouchUpInside];
        [bottomView addSubview:deleteBtn];
        saveButton.frame = CGRectMake(Screen_W/2.0+viewPix(10), viewPix(8), (Screen_W-viewPix(95))/2.0, viewPix(48));
    }
}



@end
